package com.yequan.hbb.view.fragment.ques_page_fragment;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yequan.hbb.R;
import com.yequan.hbb.adapter.DynamicAdapter;
import com.yequan.hbb.entries.DynamicEntries;
import com.yequan.hbb.presenter.DynamicPresenter;
import com.yequan.hbb.view.DynamicView;

import java.util.List;

/**
 * Created by DingLong on 2016/10/13.
 */

public class DynamicFragment extends ListFragment implements DynamicView<DynamicEntries>, SwipeRefreshLayout.OnRefreshListener {

    private DynamicAdapter adapter;
    private DynamicPresenter presenter;
    private SwipeRefreshLayout swipe;

    public DynamicFragment() {
    }

    public static DynamicFragment newInstance(String title) {
        Bundle args = new Bundle();
        args.putString("title", title);
        DynamicFragment fragment = new DynamicFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_dynamic, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        swipe = (SwipeRefreshLayout) view.findViewById(R.id.dynamic_swipe);
        Bundle bundle = getArguments();
        String title = bundle.getString("title");
        setEmptyText(title);
        adapter = new DynamicAdapter(view.getContext());
        setListAdapter(adapter);
        presenter = new DynamicPresenter(view.getContext());
        presenter.attach(this);
        presenter.getData(0, 10);
        swipe.setColorSchemeResources(R.color.colorAccent, R.color.colorYellow, R.color.colorBlack, R.color.colorPrimary);
        swipe.setSize(100);
        swipe.setOnRefreshListener(this);
    }

    @Override
    public void showDynamic(DynamicEntries dynamicEntries) {
        List<DynamicEntries.DataBean.ContentBean.DataBean2> data = dynamicEntries.getData().getContent().getData();
        adapter.addAll(data);
    }

    @Override
    public void setEmptyText(CharSequence text) {
        TextView emptyView = (TextView) getListView().getEmptyView();
        emptyView.setVisibility(View.GONE);
        emptyView.setText(text);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.dettch();
    }

    @Override
    public void onRefresh() {
        presenter.getData(0, 10);
        swipe.setRefreshing(false);
    }
}
